package com.example.hello_world2;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "ooo--->";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    /**
     * 对话框
     * @param view
     */
    public void commondialog( View view){
        // 1.创建构建器
        // 2.给构建器设置属性，标题，内容，按钮
        // 3.创建一个dialog
        // 4.显示对话框
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        builder.setCancelable(false); // 屏蔽了回退键
        builder.setTitle("中华小当家");
        builder.setMessage("你是嘟嘟吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){



            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: 确定");
            }
        });
        builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: 隐藏");
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Log.d(TAG, "onClick: 取消");
            }
        });

       builder.show();
    }

    /**
     * 下拉选项
     * @param view
     */
    public void selectdialog(View view){
        final String[] items = {"java","C#","Php","python"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setTitle("选择语言！");
        builder.setItems(items,new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: "+ which);
                Toast.makeText(getApplicationContext(), "选择的是:" + items[which], Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 单选对话框的效果
     * @param view
     */
    public void singledialog(View view){
        final String[] items = {"java","C#","Php","python"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("选择语言！");
        builder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: "+ which);
                Toast.makeText(getApplicationContext(), "选择的是:" + items[which], Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: 确定");
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 多选对话框
     * @param view
     */
    public void chackout(View view){
        final String[] items = {"java","C#","Php","python"};
        final boolean[] checkedItems = {true,false,false,false};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("选择语言！");
        builder.setMultiChoiceItems(items,checkedItems,new DialogInterface.OnMultiChoiceClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Log.d(TAG, "onClick: "+ which);
                Toast.makeText(getApplicationContext(), "选择的是:" + items[which], Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "onClick: 确定");
                for (boolean i: checkedItems){
                    Log.d(TAG, "选择了"+ i);
                }
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 自定义对话框
     * @param
     * @return
     */
    public void customdialog(View v){
        LayoutInflater m =  LayoutInflater.from(this);
        View view = m.inflate(R.layout.custom,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("请输入密码！");
        builder.setCancelable(false);
        builder.setView(view);

        final AlertDialog dialog=builder.create();
        dialog.show();

        Button qdbutton = (Button) view.findViewById(R.id.qdbutton); // 确定按钮
        Button qxbutton = (Button) view.findViewById(R.id.qxbutton); // 取消按钮

        qdbutton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                dialog.dismiss(); // 让对话框消失
            }
        });
        qxbutton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                dialog.dismiss(); // 让对话框消失
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
